package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.AccountDiscount;
import com.tbit.uqbike.object.pojo.BrandDiscountConfig;
import com.tbit.uqbike.object.pojo.dto.BrandDiscountConfigDTO;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.BrandDiscountConfigService;
import com.tbit.uqbike.webmanager.dao.core.AccountDiscountDao;
import com.tbit.uqbike.webmanager.dao.core.BrandDiscountConfigDao;
import com.tbit.uqbike.webmanager.util.StringUtils;
import com.tbit.utils.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author:BUGTian
 * @DATE: 2022/9/21
 */
@Service
public class BrandDiscountConfigServiceImpl extends ServiceImpl<BrandDiscountConfigDao, BrandDiscountConfig> implements BrandDiscountConfigService {

    @Autowired
    private AccountDiscountDao accountDiscountDao;

    @Autowired
    private BrandDiscountConfigDao brandDiscountConfigDao;

    @Override
    public Result insert(BrandDiscountConfigDTO brandDiscountConfigDTO) {
        String name = brandDiscountConfigDTO.getName();
        Integer accountId = brandDiscountConfigDTO.getAccountId();
        if (accountId == null || StringUtils.isEmpty(name)) {
            return Result.error("参数有误");
        }
        BrandDiscountConfig config = getOne(new QueryWrapper<BrandDiscountConfig>().eq("name", name).eq("accountId", accountId));
        if (config != null) {
            return Result.error("名称重复");
        }
        BrandDiscountConfig brandDiscountConfig = new BrandDiscountConfig();
        BeanUtils.copyProperties(brandDiscountConfigDTO, brandDiscountConfig);
        brandDiscountConfig.setAddTime(new Date());
        save(brandDiscountConfig);
        brandDiscountConfig.setType(brandDiscountConfig.getConfigId());
        checkLength(brandDiscountConfig);
        updateById(brandDiscountConfig);
        return Result.success();
    }

    @Override
    public Result delete(Integer configId) {
        Assert.notNull(configId, "参数有误");
        if (accountDiscountDao.selectCount(new QueryWrapper<AccountDiscount>().eq("configId", configId)) > 0) {
            return Result.error("该类型已有区域配置");
        }
        removeById(configId);
        return Result.success();
    }

    @Override
    public List<BrandDiscountConfig> getAll() {
        return brandDiscountConfigDao.getAll();
    }

    void checkLength(BrandDiscountConfig brandDiscountConfig){
        if (StrUtil.isNotEmpty(brandDiscountConfig.getName()) && brandDiscountConfig.getName().length() > 100){
            throw new BaseException("认证类型名称过长");
        }else if (StrUtil.isNotEmpty(brandDiscountConfig.getRemark()) && brandDiscountConfig.getRemark().length() > 255){
            throw new BaseException("认证类型备注过长");
        }
    }
}
